package jay.home.bet.web.struts;

import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jay.home.bet.domain.User;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class AddUserAction extends BaseAction
{
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		
		boolean b = Pattern.matches( "[a-zA-Z0-9]{6,18}", username );
		
		if (!b)
		{
			request.setAttribute("error", "用户名不合法,请重新输入!用户名只可为数字和大小写字母且长度在6位到18位之间!");
			return mapping.getInputForward();
		}
		
		User user = getaccountService().getUser(username);
		
		if ( user == null || user.getUsername() == null || user.getUsername().equals(""))
		{
			User newUser = new User();
			newUser.setUsername(username);
			newUser.setPassword(password);
			newUser.setActive(false);
			newUser.setBalance(new Double(0));
			newUser.setOnline(false);
			
			getaccountService().insertUser(newUser);
			
			request.setAttribute("info", "注册成功,请等待激活!");
			return mapping.findForward("SUCCESS");
		}
		else
		{
			request.setAttribute("error", "用户名已存在,请重新输入!");
			return mapping.getInputForward();
		}
	}
}

